package com.lt.test.consumer.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lt.test.api.KtPurchaseOrderService;
import com.lt.test.common.constant.HttpRequestConstant;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.ExcelUtil;
import com.lt.test.common.util.HttpClientUtil;
import com.lt.test.common.util.WebUtils;
import com.lt.test.entity.DTO.ActiveUser;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.entity.DTO.ResponseDTO;
import com.lt.test.entity.QueryModel.MapQueryModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ktPurchase")
public class KtPurchaseOrderController {
	private static final Logger logger = LoggerFactory.getLogger(KtPurchaseOrderController.class);
    @Reference(version = "1.0.0")
    private KtPurchaseOrderService ktPurchaseOrderService;

    /**
     * 分页采购订单信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getPurchaseOrder")
    public
    @ResponseBody
    PageDTO<Map<String, Object>> getProductType(HttpServletRequest request, HttpServletResponse response) {
    	 Map<String,String> parameter = WebUtils.getAllParamMap(request);
        System.out.println(parameter);
    	 PageDTO<Map<String, Object>> result = ktPurchaseOrderService.getPurchaseListByPage(parameter);
        return result;
    }

    /**
     * 采购订单详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getPurchaseItems")
    public
    @ResponseBody
    List<Map<String, Object>> getPurchaseItems(HttpServletRequest request, HttpServletResponse response) {
    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
    	List<Map<String, Object>> result = ktPurchaseOrderService.getPurchaseItems(parameter);
        return result;
    }

    /**
     * 供应商修改信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/getSupplier")
    public @ResponseBody
    ResponseDTO  getScanCode(HttpServletRequest request) throws Exception {
        request.setCharacterEncoding("utf-8");
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        String id = parameter.get("id") == null ? "" : parameter.get("id").toString();
        ResponseDTO responseDTO = new ResponseDTO();
        Map data = ktPurchaseOrderService.findById(id);
        responseDTO.setCode(HttpRequestConstant.REQUEST_SUCCESS_CODE);
        responseDTO.setMessage("获取扫码出货成功");
        responseDTO.setSuccess(true);
        responseDTO.setData(data);
        return responseDTO;
    }

    /**
     * 分页供应商信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getSupplierListByPage")
    public
    @ResponseBody
    PageDTO<Map<String, Object>> getSupplierListByPage(HttpServletRequest request, HttpServletResponse response) {
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        PageDTO<Map<String, Object>> result = ktPurchaseOrderService.getSupplierListByPage(parameter);
        return result;
    }

    /**
     * 保存供应商
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/saveSupplier")
    public
    @ResponseBody
    ResponseDTO saveSupplier(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.saveSupplier(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 删除供应商
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/deleteSupplier")
    public
    @ResponseBody
    ResponseDTO deleteSupplier(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        String id = request.getParameter("id");
        try {
            ktPurchaseOrderService.deleteSupplier(id);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 保存采购单
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/savePurchaseOrder")
    public
    @ResponseBody
    ResponseDTO savePurchaseOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.savePurchaseOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 采购单审核收货
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/purchaseOrderCheck")
    public
    @ResponseBody
    ResponseDTO purchaseOrderCheck(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.purchaseOrderCheck(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 保存要货单
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/saveShopRequireOrder")
    public
    @ResponseBody
    ResponseDTO saveShopRequireOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.saveShopRequireOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 更新要货单商品确认数据
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/upShopRequireOrderItems")
    public
    @ResponseBody
    ResponseDTO upShopRequireOrderItems(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            parameter.put("operator", activeUser.getUserId());
            ktPurchaseOrderService.upShopRequireOrderItems(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }



    /**
     * 要货订单审核收货
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopRequireOrderCheck")
    public
    @ResponseBody
    ResponseDTO shopRequireOrderCheck(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.shopRequireOrderCheck(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 分页要货订单信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopRequireOrderListByPage")
    public
    @ResponseBody
    PageDTO<Map<String, Object>> getShopRequireOrderListByPage(HttpServletRequest request, HttpServletResponse response) {
    	 Map<String,String> parameter = WebUtils.getAllParamMap(request);

        PageDTO<Map<String, Object>> result = ktPurchaseOrderService.getShopRequireOrderListByPage(parameter);
        System.out.println(request);
        return result;
    }


    /**
     * 要货订单详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopRequireOrderItems")
    public
    @ResponseBody
    List<Map<String, Object>> getShopRequireOrderItems(HttpServletRequest request, HttpServletResponse response) {
    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
    	List<Map<String, Object>> result = ktPurchaseOrderService.getShopRequireOrderItems(parameter);
        return result;
    }


    /**
     * 店铺到货确认
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopConfirmDeliverOrder")
    public
    @ResponseBody
    ResponseDTO shopConfirmDeliverOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.shopConfirmDeliverOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 保存出货单
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/saveShopOutstockOrder")
    public
    @ResponseBody
    ResponseDTO saveShopOutstockOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.saveShopOutstockOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 出货订单审核
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopOutstockOrderCheck")
    public
    @ResponseBody
    ResponseDTO shopOutstockOrderCheck(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.shopOutstockOrderCheck(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 分页出货订单信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopOutstockOrderListByPage")
    public
    @ResponseBody
    PageDTO<Map<String, Object>> getShopOutstockOrderListByPage(HttpServletRequest request, HttpServletResponse response) {
    	 Map<String,String> parameter = WebUtils.getAllParamMap(request);
        PageDTO<Map<String, Object>> result = ktPurchaseOrderService.getShopOutstockOrderListByPage(parameter);
        System.out.println(request);
        return result;
    }


    /**
     * 出货订单详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopOutstockOrderItems")
    public
    @ResponseBody
    List<Map<String, Object>> getShopOutstockOrderItems(HttpServletRequest request, HttpServletResponse response) {
    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
    	List<Map<String, Object>> result = ktPurchaseOrderService.getShopOutstockOrderItems(parameter);
        return result;
    }


    /**
     * 店铺出货确认
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopConfirmOutstockOrder")
    public
    @ResponseBody
    ResponseDTO shopConfirmOutstockOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
	    	Subject subject = SecurityUtils.getSubject();
	        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	        parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.shopConfirmOutstockOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        }catch (CustomException e) {
			responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
			responseDTO.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }



    /**
     * 保存入库单
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/saveShopInstockOrder")
    public
    @ResponseBody
    ResponseDTO saveShopInstockOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
	        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	        parameter.put("operator", activeUser.getUserId());
        	ktPurchaseOrderService.saveShopInstockOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 入库订单审核
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopInstockOrderCheck")
    public
    @ResponseBody
    ResponseDTO shopInstockOrderCheck(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.shopInstockOrderCheck(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 分页入库订单信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopInstockOrderListByPage")
    public
    @ResponseBody
    PageDTO<Map<String, Object>> getShopInstockOrderListByPage(HttpServletRequest request, HttpServletResponse response) {
    	 Map<String,String> parameter = WebUtils.getAllParamMap(request);
        PageDTO<Map<String, Object>> result = ktPurchaseOrderService.getShopInstockOrderListByPage(parameter);
        System.out.println(request);
        return result;
    }


    /**
     * 入库订单详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getShopInstockOrderItems")
    public
    @ResponseBody
    List<Map<String, Object>> getShopInstockOrderItems(HttpServletRequest request, HttpServletResponse response) {
    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
    	List<Map<String, Object>> result = ktPurchaseOrderService.getShopInstockOrderItems(parameter);
        return result;
    }


    /**
     * 店铺入库确认
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/shopConfirmInstockOrder")
    public
    @ResponseBody
    ResponseDTO shopConfirmInstockOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	Subject subject = SecurityUtils.getSubject();
	        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	        parameter.put("operator", activeUser.getUserId());
	        System.out.println("============>店铺入库确认:"+parameter);
        	ktPurchaseOrderService.shopConfirmInstockOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        }catch (CustomException e) {
			responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
			responseDTO.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }

    /**
     * 生成出入库订单
     *
     * @param request
     * @param response
     * @return json
     */
    @RequestMapping("/createInOutStockOrder")
    public
    @ResponseBody
    ResponseDTO createInOutStockOrder(HttpServletRequest request, HttpServletResponse response) {
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String,String> parameter = WebUtils.getAllParamMap(request);
        try {
        	ktPurchaseOrderService.createInOutStockOrder(parameter);
            responseDTO.setCode("200");
            responseDTO.setSuccess(true);
            responseDTO.setMessage("操作成功");
        }catch (CustomException e) {
			responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
			responseDTO.setMessage(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
            e.printStackTrace();
            responseDTO.setCode("500");
            responseDTO.setMessage("服务器异常");
        }
        return responseDTO;

    }


    /**
     * 图片上传
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "/submitImg", method = {RequestMethod.GET, RequestMethod.POST})
//    public @ResponseBody
//    ResponseDTO submitImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        System.out.println("===============图片上传开始=============");
//        ResponseDTO responseDTO = new ResponseDTO();
//        Map<String, Object> result = new HashMap<String, Object>();
//        JSONObject obj = new JSONObject();
//        //图片上传
//        String fileStr = request.getParameter("fileStr");
//        String fileType = request.getParameter("fileType");
//        String fileStart = request.getParameter("fileStart");
//        // 图片上传
//        if (fileStr != null && fileType != null) {
//            Map<String, String> data = new HashMap<>();
//            data.put("fileStr", fileStr);
//            data.put("fileType", fileType);
////            String url = "http://app1:8080/clouderp-upload-rest/api/upload/upload";
//            String url = "http://clouderp.skystorechain.com:20080/clouderp-upload-rest/api/upload/upload";
//            String responseResult = HttpClientUtil.sendPostRequest(url, data, null, null);
//            JSONObject jsonObject = JSONObject.fromObject(responseResult);
//            if (jsonObject == null || jsonObject.isEmpty() || !"000".equals(jsonObject.get("code"))){
//                responseDTO.setSuccess(false);
//                responseDTO.setMessage("图片上传失败异常！");
//                System.out.println("上传图片失败，错误信息为："+responseResult);
//                return responseDTO;
//            }
//            String imgUrl = jsonObject.getString("data");
//            obj.put("imgUrl",imgUrl);
////            cloudMessage.put("imgUrl",imgUrl);
//            System.out.println("====================="+jsonObject);
//            // obj.put("imgUrl","");
//            //更新图片地址
//            Map parameter = new HashMap();
//            if (fileStart.equals("1")){
//                parameter.put("businessLicenseImage", imgUrl);
//            }else if (fileStart.equals("2")){
//                parameter.put("licenseImage", imgUrl);
//            }else if(fileStart.equals("3")){
//                responseDTO.setData(imgUrl);
//            	responseDTO.setSuccess(true);
//                responseDTO.setMessage("图片上传成功");
//                return responseDTO;
//            }
//            boolean fruset = Boolean.parseBoolean(request.getParameter("fruset"));
//            if (fruset) {
//                String id = request.getParameter("id");
//                parameter.put("id", id);
//                ktPurchaseOrderService.saveSupplier(parameter);
//            }
//            responseDTO.setData(imgUrl);
//        }
//        responseDTO.setSuccess(true);
//        responseDTO.setMessage("图片上传成功");
//
//        return responseDTO;
//    }



    /**
     * 图片上传
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/submitImg", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    ResponseDTO submitImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("===============图片上传开始=============");
        ResponseDTO responseDTO = new ResponseDTO();
        Map<String, Object> result = new HashMap<String, Object>();
        JSONObject obj = new JSONObject();
        //图片上传
        String fileStr = request.getParameter("fileStr");
        String fileType = request.getParameter("fileType");
        // 图片上传
        if (fileStr != null && fileType != null) {
            Map<String, String> data = new HashMap<>();
            data.put("fileStr", fileStr);
            data.put("fileType", fileType);
//            String url = "http://app1:8080/clouderp-upload-rest/api/upload/upload";
            String url = "http://clouderp.skystorechain.com:20080/clouderp-upload-rest/api/upload/upload";
            String responseResult = HttpClientUtil.sendPostRequest(url, data, null, null);
            JSONObject jsonObject = JSONObject.fromObject(responseResult);
            if (jsonObject == null || jsonObject.isEmpty() || !"000".equals(jsonObject.get("code"))){
                responseDTO.setSuccess(false);
                responseDTO.setMessage("图片上传失败异常！");
                System.out.println("上传图片失败，错误信息为："+responseResult);
                return responseDTO;
            }
            String imgUrl = jsonObject.getString("data");
            obj.put("imgUrl",imgUrl);
            System.out.println("====================="+jsonObject);
            responseDTO.setCode("200");
            responseDTO.setData(imgUrl);
        }
        responseDTO.setSuccess(true);
        responseDTO.setMessage("图片上传成功");

        return responseDTO;
    }


    /**
     * 数据导出到excel
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, MapQueryModel params) throws Exception {
        logger.info("开始生成excel表格");
        String exportExcelData = request.getParameter("exportExcelData");
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        String tableType = request.getParameter("tableType");

        if (tableType == null) {
            throw new CustomException("表格数据太大");
        }

        JSONArray jsonList = JSONArray.fromObject(exportExcelData);
        List<Map<String, Object>> ShipmentOrderList = JSONArray.toList(jsonList, new HashMap<>(), new JsonConfig());
        logger.info("json转List完毕");
        Map<String, Object> map = new HashMap<>();
        logger.info("tableType is : " + tableType);

        String[] Keys = new String[0];
        String[] columnNames = new String[0];

        String fileName = "";

        // 对用户角色权限进行分析处理
//        Subject subject = SecurityUtils.getSubject();
//        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
//        params.getStringMap().put("channelCode", activeUser.getChannelName());
//
//        String quXianName = params.getStringMap().get("quXianName");
//
//        if (StringUtils.isNot_NullOrTrimEmpty(activeUser.getEmpName())){
//            params.getStringMap().put("empName", activeUser.getEmpName());
//        }
//
//        if (!StringUtils.isNullOrTrimEmpty(activeUser.getQuXianName())) {
//            quXianName = activeUser.getQuXianName();
//        }
//
//        if (!StringUtils.isNullOrTrimEmpty(quXianName)) {
//            String channelId = sPLOrderService.getIdByChannleName(quXianName);
//            params.getStringMap().put("parentChannelId", channelId == null ? "-99999" : channelId);
//        }

//        String channelName = activeUser.getRealChannelName();
//        if (!StringUtils.isNullOrTrimEmpty(channelName)) {
//            params.getStringMap().put("channelName", channelName);
//        }

        //获取导出数据
        Integer pages = 1;
        Integer pageSizes = 10000;
        //根据导出类型获取导出数据
        switch (tableType) {
            case "orderInfo": //汇总数据
                ShipmentOrderList.clear();
                map.put("sheetName", "采购订单详情数据");

                Keys = new String[]{"productName","productCode","unit","spec","cstPrice","quantity","realQuantity", "amt", "supplierName","shopName"};
                columnNames = new String[]{"商品名称", "商品编码", "单位", "规格", "单价", "采购数量", "实际数量", "金额", "供应商", "要货方",};
                fileName = "采购订单详情数据";

                params.getStringMap().put("pageSize", pageSizes.toString());
                params.getStringMap().put("id", id);
                while (true) {
                    params.getStringMap().put("page", pages.toString());
                    List<Map<String, Object>> resultData = ktPurchaseOrderService.getPurchaseItems(params.getStringMap());
                    ShipmentOrderList.addAll(resultData);
                    if (resultData == null || resultData.size() < pageSizes) {
                        break;
                    }
                    pages++;
                }
                break;
            case "enter": //入库数据
                ShipmentOrderList.clear();
                map.put("sheetName", "入库详情数据");


                Keys = new String[]{"id","selfCode", "productCode","productName", "unit","spec", "quantity","realQuantity","creationDate","10",};
                columnNames = new String[]{"订单号","自编码","商品条码","品名","单位","规格","数量","实际入库数量","生产日期","备注",};
                fileName = "采购订单详情数据";

                params.getStringMap().put("pageSize", pageSizes.toString());
                params.getStringMap().put("id", id);
                params.getStringMap().put("type", type);
                while (true) {
                    params.getStringMap().put("page", pages.toString());
                    System.out.println("===========入库详情请求数据=======>"+params.getStringMap());
                    List<Map<String, Object>> resultData = ktPurchaseOrderService.getShopInstockOrderItems(params.getStringMap());
                    System.out.println("===========入库详情数据=======>"+resultData);
                    ShipmentOrderList.addAll(resultData);
                    if (resultData == null || resultData.size() < pageSizes) {
                        break;
                    }
                    pages++;
                }
                break;
        }
        logger.info("添加sheetName的Map");
        ShipmentOrderList.add(0, map);
        logger.info("开始执行ExcelUtil.createHSSFWorkbook方法");
        HSSFWorkbook wb;
        try {
            wb = ExcelUtil.createHSSFWorkbook2(ShipmentOrderList, Keys, columnNames);
        }catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new CustomException("数据实在太大，建议分多次导出");
        }

        logger.info("执行ExcelUtil.createHSSFWorkbook方法完毕");
        OutputStream out = response.getOutputStream();
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        response.setContentType("application/msexcel;charset=UTF-8");
        logger.info("设置响应信息完成");
        //输出excel文件
        wb.write(out);
        out.close();
        logger.info("结束生成excel表格");
    }
}
